#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;


vector<vector<int>> fourSum(vector<int>& nums, int target) {
    sort(nums.begin(), nums.end());
    vector<vector<int>> res;
    int n = nums.size();
    for (int a = 0; a < n - 3;)
    {
        for (int b = a + 1; b < n - 2;)
        {
            int left = b+1, right = n - 1;
            while (left < right)
            {
                int num = target - nums[a] - nums[b];
                int var = nums[left] + nums[right];
                if (var == num)
                {
                    res.push_back({ nums[a],nums[b],nums[left],nums[right] });
                    left++;
                    right--;
                    while (left < right && nums[left - 1] == nums[left]) left++;
                    while (left < right && nums[right + 1] == nums[right]) right--;

                }
                else if (var > num) right--;
                else left++;
            }
            b++;
            while (b < n - 2 && nums[b - 1] == nums[b]) b++;
        }
        a++;
        while (a < n - 3 && nums[a - 1] == nums[a]) a++;
    }
    return res;
}


int main()
{
    vector<int> nums = { 1,0,-1,0,-2,2 };
    fourSum(nums, 0);
    return 0;
}